<?php

function check_delete_disable($id){
	$history_jabatan = my_get_data_by_id('karyawan_history_jabatan','history_jabatan_id',$id);
	$query = "SELECT * FROM karyawan_history_jabatan WHERE karyawan_id = {$history_jabatan['new_karyawan_id']}";
	$res = my_query($query);
	if(my_num_rows($res) > 0){
		return false;
	}
	return true;
}


function detail_karyawan_mutasi($id){
	$view = form_header( "seleski" , "seleski"  );
	
 
	$fields = my_get_data_by_id('karyawan_history_jabatan','history_jabatan_id',$id);
	 
	$view .= form_field_display( $fields['nomor']  , "Nomor SK"  ); 
	$view .= form_field_display( date('d-m-Y',strtotime($fields['tanggal_efektif'])) , "Tanggal Efektif" ); 
	
	$karyawans = loaddata_karyawan_start($fields['karyawan_id']);
	$view .= form_field_display( $karyawans['nik'].'/'.$karyawans['nama']    , "NIK/Nama Karyawan"  );	 
	
	$jabatan_data = loaddata_jabatan($karyawans['jabatan_id']);
	$view .= form_field_display( $jabatan_data['nomor_posisi'] .'/'.$jabatan_data['nama_jabatan']    , "Jabatan"  );	 
	$view .= form_field_display( $karyawans['golongan_jabatan'] , "Golongan Jabatan"    ); 
	$view .= form_field_display( $karyawans['grade_id']  , "Grade"  );  	 
	$view .= form_field_display( $karyawans['gapok_km']  , "Gaji Pokok"    );
	$view .= form_footer( );
	return $view;
}

function list_seleksi_mutasi(){
my_set_code_js('
		function confirmDelete( i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+i;
		}
		return;	}
	');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'), 
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Efektif'=>array('style'=>'border-bottom:2px solid;width:10%'),   
		'Jabatan Lama'=>array('style'=>'border-bottom:2px solid;width:21%'),  
		'Jabatan Baru'=>array('style'=>'border-bottom:2px solid;width:21%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:8%'), 
	);

	$query = "SELECT * FROM karyawan_history_jabatan ";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;   
	$per_page =34;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	 
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 34; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=edit&sid='.$ey['history_jabatan_id'],
				'rel'=>'facebox',
				'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=detail&id='.$ey['history_jabatan_id'],
				'rel'=>'facebox',
				'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['history_jabatan_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  strtoupper($ey['nik'] ),   
			'nam'=>  $karyawan['nama'] ,   
			'tgl_efektif'=>   position_text_align(      date( "d-m-Y" , strtotime($ey['tanggal_efektif'] ))  , 'left') , 
			'lama'=>   position_text_align(  $ey['jabatan_name']  , 'left') , 
			'baru'=>   position_text_align(  $ey['jabatan_baru']  , 'left') , 
			'operasi'=> position_text_align(  $detail_button.' '. (check_delete_disable($ey['history_jabatan_id']) ? $delete_button : '') , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	if(is_admin()):
		$menuontop = array(  
			'Tambah data'=>array('onclick'=>'javascript:location=\'index.php?com='.$_GET['com'].'&task=form_seleksi_mutasi&id=0\''),
		);
	else:
		$menuontop = array(  
			'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=seleksi_internal\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
		);
	endif;	
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  6 , false , $paging );
} 


function form_seleksi_mutasi( $id ){
	 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' ,
			'components/system/jquery/autocomplete/jquery.autocomplete.js'
		)
	); 
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);
my_set_code_js('  
			function findValue(li) {
				if( li == null ) return alert("No match!"); 
				if( !!li.extra ) var sValue = li.extra[0]; 
				else var sValue = li.selectValue;
			}

			function selectItem(li) {
				findValue(li);
			}

			function formatItem(row) {
				return   row[0] ;
			}

			function lookupAjax(){
				var oSuggest = $("#nama_karyawan")[0].autocompleter;
				oSuggest.findValue(); 
				return false;
			}
			
			$(document).ready(function() {
				$("#nama_karyawan").autocomplete(
					"autocomplete_daftar_karyawan.php",
					{
						delay:10,
						minChars:2,
						matchSubset:1,
						matchContains:1,
						cacheLength:5,
						onItemSelect:selectItem,
						onFindValue:findValue,
						formatItem:formatItem,
						autoFill:true
					}
				);
				 
			});
			

			function checkName(name){
				$.get("check_karyawan_names.php", { nama: name  },
				   function(data){
					 if(data ==  \'0\' ){
						alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
						$(\'#nama_karyawan\').val(\'\');
						return false;
					 } 
				   });
			}
			
			function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

	function selectItem(li) {
		findValue(li);
	}

	function formatItem(row) {
		return   row[0] ;
		}
 
	$(document).ready(function() {
		$("#nama_jabatan").autocomplete(
			"autocomplete_daftar_jabatan.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:5,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});

	function checkNameJabatan(name){
		$.get("check_jabatan_names.php", { nama: name  },
		function(data){
			if(data ==  \'0\' ){
				alert(\'Jabatan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\'); 
				$(\'#nama_jabatan\').val(\'\');
				return false;
			} 
		});
	}
		');
	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('karyawan_history_jabatan','history_jabatan_id',$id);
	
	$nomor = array(
		'name'=>'nomor',
		'value'=>(isset($_POST['nomor'])? $_POST['nomor'] : $fields['nomor']),
		'id'=>'nomor',
		'type'=>'textfield',
		'size'=>'35'
	);
	$form_nomor = form_dynamic($nomor);
	$view .= form_field_display( $form_nomor  , "Nomor SK"  );
	$ftanggalm = $ftanggal = date('d-m-Y');
	
	if($fields){ 
		list($yyyytanggalm , $mmtanggalm,$ddtanggalm ) = explode("-" ,$fields['tanggal_efektif'] ); 
		$ftanggalm = $ddtanggalm.'-'.$mmtanggalm.'-'.$yyyytanggalm;
	}
	 
	$tanggal_efektif = array(
			'name'=>'tanggal_efektif',
			'value'=>(isset($_POST['tanggal_efektif'])? $_POST['tanggal_efektif'] : $ftanggalm),
			'id'=>'tanggal_efektif' 
		);
	$form_tanggal_efektif = form_calendar($tanggal_efektif);
	$view .= form_field_display( $form_tanggal_efektif , "Tanggal Efektif" );
	
	
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : ( $id ==0 ? '' : $field_temp['nik'].'/'.$field_temp['nama'])),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'60',
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan    , "NIK/Nama Karyawan"  );	
	$jabatan = array(
		'name'=>'nama_jabatan',
		'value'=>( isset($_POST['nama_jabatan'])? $_POST['nama_jabatan'] : ( $id ==0 ? $nomor_organisasi : $field_temp['nomor_posisi']."/".$field_temp['nama_jabatan'])),
		'id'=>'nama_jabatan',
		'type'=>'textfield',
		'size'=>'60',
		'onchange'=>'javascript:checkNameJabatan(this.value)'
	);
	$form_jabatan = form_dynamic($jabatan);
	$view .= form_field_display( $form_jabatan    , "Jabatan baru"  );	
	
	$goljab_id = isset( $fields['golongan_jabatan_id'] ) ? $fields['golongan_jabatan_id']:  0;
	$query = "SELECT * FROM golongan_jabatan ORDER BY kode_golongan_jabatan ASC";
	$result = my_query($query);
	$goljabs = array();
	while( $r = my_fetch_array($result) ){
		$goljabs[$r['golongan_jabatan_id']] =$r['kode_golongan_jabatan'];
	}	
	$golongan_jabatan_id = array(
		'name'=>'golongan_jabatan_id',
		'value'=>( isset($_POST['golongan_jabatan_id'] ) ? $_POST['golongan_jabatan_id'] : $goljab_id  ),
		'id'=>'golongan_jabatan_id' 
		//'onchange'=>'javascript:checkNipar();'		
	);
	$form_temp = form_dropdown($golongan_jabatan_id , $goljabs );
	$view .= form_field_display( $form_temp , "Golongan Jabatan"    );

	$grd_id = isset( $fields['grade_id'] ) ? $fields['grade_id']:  0;
	$grade_id = array(
		'name'=>'grade_id',
		'value'=>( isset($_POST['grade_id'] ) ? $_POST['grade_id'] : $grd_id  ),
		'id'=>'grade_id'  
	);

	if($fields){
		$query_grade = "SELECT grade_id FROM gol_grade 
			WHERE golongan_jabatan_id=".$goljab_id." 
			ORDER BY grade_id ASC";
		$result_grade = my_query($query_grade);
		$opsi_pilihan ='';
		while($row_grade = my_fetch_array($result_grade)){
			if($row_grade['grade_id'] == $grd_id){
				$opsi_pilihan .='<option selected value="'.$row_grade['grade_id'].'">'.$row_grade['grade_id'].'</option>';
			}else{
				$opsi_pilihan .='<option value="'.$row_grade['grade_id'].'">'.$row_grade['grade_id'].'</option>';			
			}
			//var_dump($opsi_pilihan);
			$dropdown_extends = dropdown_multi_rows_extends_dua( $golongan_jabatan_id , $grade_id , "combobox_grade.php", $opsi_pilihan);
		}
	}else{	
		$dropdown_extends = dropdown_multi_rows_extends_dua( $golongan_jabatan_id , $grade_id , "combobox_grade.php", false );
	}
	$view .= form_field_display( $dropdown_extends  , "Grade"  );  	
	$gaji = array(
		'name'=>'gaji',
		'value'=>( isset($_POST['gaji'] ) ? $_POST['gaji'] : rp_format($field_temp ) ),
		'id'=>'gaji' , 
		'type'=>'text',
		'size'=>'35' ,
	);
	$form_gaji= form_money($gaji );
	$view .= form_field_display( $form_gaji  , "Gaji Pokok"    );
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function submit_seleksi_mutasi($id = 0){ 
	if($id > 0){
		 delete_seleksi_mutasi($id);
		$id = 0;
	}	
	$karyawan_id 	= check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$karyawan_data = loaddata_karyawan($karyawan_id); 
	$jabatan_data = loaddata_jabatan($karyawan_data['jabatan_id']);
		$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']	);
		$jabatan_data_new = loaddata_jabatan($jabatan_id);
	if($_POST){ 
		list($ddtanggalm , $mmtanggalm, $yyyytanggalm) = explode("-" ,$_POST['tanggal_efektif'] ); 
		$ftanggalm = $yyyytanggalm.'-'.$mmtanggalm.'-'.$ddtanggalm;
	}
	$datas = array(
		'nomor'				=>my_type_data_str($_POST['nomor']), 
		'tanggal_efektif'	=>my_type_data_str($ftanggalm),
		'karyawan_id'	 	=>my_type_data_str($karyawan_id),	
		'nik'	 			=>my_type_data_str($karyawan_data['nik']),

		
		'gapok_km_lama'	 	=>my_type_data_str($karyawan_data['gapok_km']),	
		'gapok_km_baru'	 	=>my_type_data_str($_POST['gaji']), 
		
		'golongan_jabatan_id_lama'=>my_type_data_str($karyawan_data['golongan_jabatan_id']), 
		'golongan_jabatan_id_baru'=>my_type_data_str($_POST['golongan_jabatan_id']),
		
		'grade_id_lama'			=>my_type_data_str($karyawan_data['grade_id']),   	 	 	 	 	 	
		'grade_id_baru'			=>my_type_data_str($_POST['grade_id']),   	 	 	
		
		'jabatan_id_lama'		=>my_type_data_str($karyawan_data['jabatan_id']), 
		'jabatan_id_baru'		=>my_type_data_str($jabatan_id), 
		
		
		
		'jabatan_name'	 	=>my_type_data_str($jabatan_data['nomor_posisi']."/".$jabatan_data['nama_jabatan']),	
		'jabatan_baru'	 	=>my_type_data_str($jabatan_data_new['nomor_posisi']."/".$jabatan_data_new['nama_jabatan']),	
		
		'new_karyawan_id'	 =>my_type_data_str(0),
	);
	  
	$history_jabatan_id = my_insert_record('karyawan_history_jabatan',$datas); 
	unset($karyawan_data);
	if($id == 0){ 
		$kekaryawanans = array( 
			'gapok_km'	 =>my_type_data_str($_POST['gaji']), 	 	  
			'golongan_jabatan_id'=>my_type_data_str($_POST['golongan_jabatan_id']), 
			'grade_id'		=>my_type_data_str($_POST['grade_id']),   	 	 	 	 	 	
			'jabatan_id'		=>my_type_data_str($jabatan_id), 
		);
		$test = my_update_record('kekaryawanan' , 'karyawan_id' ,$karyawan_id, $kekaryawanans); 
		create_data_karyawan_json($karyawan_id);
	}
	
	return true;
}

function delete_seleksi_mutasi($id){	
	$history_jabatan = my_get_data_by_id('karyawan_history_jabatan','history_jabatan_id',$id);
	 
	$kekaryawanans = array( 
		'gapok_km'	 		=>my_type_data_str($history_jabatan['gapok_km_lama']), 	 	  
		'golongan_jabatan_id'=>my_type_data_str($history_jabatan['golongan_jabatan_id_lama']), 
		'grade_id'		=>my_type_data_str($history_jabatan['grade_id_lama']),   	 	 	 	 	 	
		'jabatan_id'		=>my_type_data_str($history_jabatan['jabatan_id_lama']), 
	);
	$test = my_update_record('kekaryawanan' , 'karyawan_id' ,$history_jabatan['karyawan_id'], $kekaryawanans); 
	create_data_karyawan_json($history_jabatan['karyawan_id']);
	$query = "DELETE FROM karyawan_history_jabatan WHERE history_jabatan_id = {$id}";
	return my_query($query);
}    

function validasi_seleksi_mutasi(){
	$errsubmit = false;
	$err = array(); 
	
	if(trim($_POST['nomor'])  == '' ){
		$errsubmit = true;
		$err[] = "Nomor surat belum di isi";
	}
	
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);		
	if( (int)$karyawan_id == 0 ){
		$errsubmit = true;
		$err[] = "NIK/Nama karyawan belum benar";
	} 
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']	);
	if( (int) $jabatan_id == 0){
		$errsubmit = true;
		$err[] = "Jabatan belum benar";
	}
	
	if( (int)$_POST['golongan_jabatan_id'] == 0){
		$errsubmit = true;
		$err[] = "Golongan belum benar";
	}
	
	if( (int)$_POST['gaji'] == 0){
		$errsubmit = true;
		$err[] = "Gapok baru belum benar";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}